﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using rock.DAL;

namespace rock.UI.common
{
   public  class StringUtils
    {
       public const string YYYYMMDD = "yyyy年MM月dd日";
       public const string YYYYMMDDHHMMSS = "yyyy年MM月dd日HH时mm分ss秒";
       public const string YYYY_MM_DD = "yyyy-MM-dd";


       public static void checkText(String text, int minLenght, int maxLenght, String lableText)
       {
           text = text == null ? "" : text.Trim();
           if (minLenght == 0 && text.Equals(""))
           {
               return;
           }

           if (minLenght >0 &&   text.Equals(""))
           {
               throw new Exception(lableText + "值不能为空.");
           }
           if (text.Length < minLenght)
           {
               throw new Exception(lableText + "值过短.请至少输入" + minLenght+"字符.");
           }

           if (text.Length > maxLenght)
           {
               throw new Exception(lableText + "值过长.至多" + maxLenght + "字符.");
           }
       
       }
       /// <summary>
       /// 验证字符串是否只有正小数字
       /// </summary>
       /// <param name="argStr">需要验证的字符串</param>
       /// <returns>true：正常；fales：失败</returns>
       public static void checkIsWorld(string argStr, string lableText)
       {

           Regex objRegex = new Regex(@"^[a-zA-Z0-9]+$");

           if (!objRegex.IsMatch(argStr))
           {
               throw new Exception("" + lableText + "值请输入数字,大小写字母");
           }
            
       }
 
       /// <summary>
       /// 验证字符串是否只有正小数字
       /// </summary>
       /// <param name="argStr">需要验证的字符串</param>
       /// <returns>true：正常；fales：失败</returns>
       public static void checkIsDecimal(string argStr ,string lableText)
       {
         
           Regex objRegex = new Regex(@"^\d+[.]?\d*$");

           if (!objRegex.IsMatch(argStr))
           {
               throw new Exception("" + lableText + "值请输入大于零的数字.");
           }

           try
           {
               Convert.ToDecimal(argStr);
           }
           catch (Exception ex)
           {
               throw new Exception("" + lableText + "" + ex.Message);
           }
       }

        /// <summary>
       /// 验证字符串是否只有正整数字
       /// </summary>
       /// <param name="argStr">需要验证的字符串</param>
       /// <returns>true：正常；fales：失败</returns>
       public static void checkIsPositiveInteger(string argStr, string lableText)
       {
           
               Regex objRegex = new Regex(@"^\d+$");
               if (!objRegex.IsMatch(argStr))
               {
                   throw new Exception("" + lableText + "值请输入大于零的正整数.");
               }

               try
               {
                   Convert.ToInt32(argStr);
               }
               catch (Exception ex)
               {
                   throw new Exception("" + lableText + "" + ex.Message);
               }
           
       }

       /// <summary>
       /// 验证字符串是否只有负整数字 
       /// </summary>
       /// <param name="argStr">需要验证的字符串</param>
       /// <returns>true：正常；fales：失败</returns>
       public static void checkIsNegativeInt(string argStr, string lableText)
       {
           
               Regex objRegex = new Regex(@"^[-]\d+$");
               if (!objRegex.IsMatch(argStr))
               {
                   throw new Exception("" + lableText + "值请输入小于零的负整数.");
               }
               try
               {
                   Convert.ToInt32(argStr);
               }
               catch (Exception ex)
               {
                   throw new Exception("" + lableText + "" + ex.Message);
               }
       }

       /// <summary>
       /// 验证字符串是否只有负整数字 
       /// </summary>
       /// <param name="argStr">需要验证的字符串</param>
       /// <returns>true：正常；fales：失败</returns>
       public static void checkIsAllInt(string argStr, string lableText)
       {
           
               Regex objRegex = new Regex(@"^[-]?\d+$");
               if (!objRegex.IsMatch(argStr))
               {
                   throw new Exception("" + lableText + "值请输入正整数.");
               }
               try
               {
                   Convert.ToInt32(argStr);
               }
               catch (Exception ex)
               {
                   throw new Exception("" + lableText + "" + ex.Message);
               }
       }

       public  static string getContractNumber()
       {
            int NUMBER =   new DalRENT_CONTRACT().GetMaxIdByContractNumber();
            NUMBER = NUMBER + 1;
            String date = DateTime.Now.ToString("yyyyMMdd");
             String  currentNumber = "" ;
            if(NUMBER < 10 ){
                currentNumber= "000";
            }else  if(NUMBER < 100 ){
                currentNumber ="00";
            }else  if(NUMBER < 1000 ){
                 currentNumber ="0";
            }
            String contractNumber = "D" + date + "-N" + currentNumber + NUMBER;
            return contractNumber; 
       }


       public static string list2sqlinIds(List<int> ids)  {
           if(ids.Count < 0 ){
               return "-999";
           }
           String idsstr = "";
           for(int i=0 ; i<ids.Count ; i++){
               idsstr += ids[i];
               if (i < ids.Count - 1) { 
                idsstr += ",";
               }
           }
           return idsstr;

       }
    }
}
